/*
 * @Author: kly812 konglingyu812@163.com
 * @Date: 2024-01-15 14:56:29
 * @LastEditors: kly812 konglingyu812@163.com
 * @LastEditTime: 2024-01-15 18:23:13
 * @FilePath: /mystl/MYSTL/vector.h
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#ifndef MYSTL_VECTOR_H
#define MYSTL_VECTOR_H
#include "allocator.h"
#include "util.h"
#include "uninitialized.h"
#include "algorithm.h"

namespace mystl{
template <class T, class Alloc = allocator<T> >
class vector{
public:
    typedef T value_type;
    typedef T* iterator;
    typedef T* pointer;
    typedef T& reference;
    typedef size_t size_type;
    
private:
    iterator start;
    iterator finish;
    iterator mem_end;
    typedef Alloc data_allocator;

    void insert_aux(iterator position, const T& x);
    void insert_aux2(iterator position, const T& x);
    void fill_initialize(const size_type& n,const value_type& value);
    iterator allocate_and_fill(const size_type& n,const value_type& value);

public:
    //这个是vector的无参构造函数
    vector(): start(0),finish(0),mem_end(0){};
    //vector的有参构造函数
    vector(const size_type& n, const value_type& value);
    explicit vector(const size_type &n);
    vector(vector& v);
    ~vector();



    //begin（）和end（）的实现
    iterator begin();
    iterator end();

    //容量相关
    size_type size();
    size_type capacity();
    bool empty();





    //元素调整
    void push_back(const value_type& val);
    void pop_back();
    iterator insert(iterator position, const value_type& value);




};
//首先实现析构函数
template<class T, class Alloc>
vector<T,Alloc>::~vector()
{

}

//实现构造函数
//实现了输入一个数字，创建数字大小数目的vex
template<class T, class Alloc>
vector<T , Alloc>::vector(const vector::size_type& n)
{
    fill_initialize(n,T());
}


template<class T, class Alloc>
vector<T , Alloc>::vector(const vector::size_type& n,const vector::value_type& value)
{
    fill_initialize(n,value);
}

template<class T, class Alloc>
void vector<T, Alloc>::fill_initialize(const vector::size_type &n, const T &value) {
    start = allocate_and_fill(n, value);
    finish = start + n;
    mem_end = finish;
}

template<class T,class Alloc>
typename vector<T,Alloc>::iterator vector<T,Alloc>::allocate_and_fill(const size_type& n,const value_type& value)
{
    iterator result = data_allocator::allocate(n);
    std::uninitialized_fill_n(result, n, value);
    return result;
} 

template<class T,class Alloc>
typename vector<T,Alloc>::iterator vector<T,Alloc>::begin()
{
    return start;
}

template<class T,class Alloc>
typename vector<T,Alloc>::iterator vector<T,Alloc>::end()
{
    return finish;
}


//实现数据的个数
template<class T, class Alloc>
typename vector<T , Alloc>::size_type vector<T,Alloc>::size()
{
    return finish-start;
}

//实现容量大小
template<class T, class Alloc>
typename vector<T , Alloc>::size_type vector<T,Alloc>::capacity()
{
    return mem_end-begin();
}
//是否为空
template<class T, class Alloc>
bool vector<T,Alloc>::empty()
{
    return begin()==end();
}


//push—back实现
template<class T,class Alloc>
void vector<T,Alloc>::push_back(const value_type& val)
{
    insert_aux2(finish,val);

}
//pop_back
template<class T,class Alloc>
void vector<T,Alloc>::pop_back()
{
    finish--;
    destroy(finish);
}



template<class T, class Alloc>
typename vector<T, Alloc>::iterator vector<T, Alloc>::insert(vector::iterator position, const value_type &value) {
    auto delta = position - start;
    insert_aux(position, value);
    return start + delta;
}



//在特定位置插入元素
template<class T, class Alloc>
void vector<T,Alloc>::insert_aux(vector::iterator position, const T& x)
{
    if(finish!=mem_end)
    {
        auto new_end = finish;
        std::cout<<std::endl;
        construct(new_end,*(finish-1));
        ++new_end;
        T tep = x;
        mystl::back_copy(position,finish-1,finish);
        *position = tep;
        std::cout<<"position"<<*position<<std::endl;
        finish = new_end;
    }
    //没有空闲的位置了，扩大进行分配
    else{
        const auto old_size = size();
        const auto new_size = old_size == 0 ? 1 : old_size*2;
        iterator new_start = data_allocator::allocate(new_size);
        iterator new_finish = mystl::uninitialized_copy(start,position,new_start);
        construct(new_finish,x);
        ++new_finish;
        new_finish = mystl::uninitialized_copy(position,finish,new_finish);
        data_allocator::destroy(start,finish);
        start = new_start;
        finish = new_finish;
        mem_end = start + new_size;
    }
}


template<class T, class Alloc>
void vector<T,Alloc>::insert_aux2(vector::iterator position, const T& x)
{
    if(finish!=mem_end)
    {
        auto new_end = finish;
        std::cout<<std::endl;
        construct(new_end,*(finish-1));
        ++new_end;
        T tep = x;
        mystl::back_copy(position,finish-1,finish);
        *position = tep;
        std::cout<<"position"<<*position<<std::endl;
        finish = new_end;
    }
    //没有空闲的位置了，扩大进行分配
    else{
        const auto old_size = size();
        const auto new_size = old_size == 0 ? 1 : old_size*2;
        iterator new_start = data_allocator::allocate(new_size);
        iterator new_finish = mystl::uninitialized_copy(start,position,new_start);
        construct(new_finish,x);
        ++new_finish;
        new_finish = mystl::uninitialized_copy(position,finish,new_finish);
        data_allocator::destroy(start,finish);
        start = new_start;
        finish = new_finish;
        mem_end = start + new_size;
    }
}










}
#endif 